{
 "cells": [
  {
   "cell_type": "code",
   "id": "initial_id",
   "metadata": {
    "collapsed": true,
    "ExecuteTime": {
     "end_time": "2025-09-30T02:41:46.332011Z",
     "start_time": "2025-09-30T02:41:45.970224Z"
    }
   },
   "source": [
    "import pandas as pd\n",
    "\n",
    "df = pd.read_csv('univ.csv', encoding='gbk')\n",
    "df"
   ],
   "outputs": [
    {
     "data": {
      "text/plain": [
       "     名次    学校名称      总分  类型 所在省份 所在城市     办学方向 主管部门\n",
       "0     1    北京大学  100.00  综合   北京  北京市    中国研究型  教育部\n",
       "1     2    清华大学   98.50  理工   北京  北京市    中国研究型  教育部\n",
       "2     3    复旦大学   82.79  综合   上海  上海市    中国研究型  教育部\n",
       "3     4    武汉大学   82.43  综合   湖北  武汉市    中国研究型  教育部\n",
       "4     5    浙江大学   82.38  综合   浙江  杭州市    中国研究型  教育部\n",
       "..  ...     ...     ...  ..  ...  ...      ...  ...\n",
       "95   96  浙江师范大学   63.37  师范   浙江  金华市  区域特色研究型  浙江省\n",
       "96   97    安徽大学   63.34  综合   安徽  合肥市    区域研究型  安徽省\n",
       "97   98  首都医科大学   63.32  医药   北京  北京市  区域特色研究型  北京市\n",
       "98   99    江南大学   63.31  综合   江苏  无锡市  区域特色研究型  教育部\n",
       "99  100    山西大学   63.29  综合   山西  太原市    区域研究型  山西省\n",
       "\n",
       "[100 rows x 8 columns]"
      ],
      "text/html": [
       "<div>\n",
       "<style scoped>\n",
       "    .dataframe tbody tr th:only-of-type {\n",
       "        vertical-align: middle;\n",
       "    }\n",
       "\n",
       "    .dataframe tbody tr th {\n",
       "        vertical-align: top;\n",
       "    }\n",
       "\n",
       "    .dataframe thead th {\n",
       "        text-align: right;\n",
       "    }\n",
       "</style>\n",
       "<table border=\"1\" class=\"dataframe\">\n",
       "  <thead>\n",
       "    <tr style=\"text-align: right;\">\n",
       "      <th></th>\n",
       "      <th>名次</th>\n",
       "      <th>学校名称</th>\n",
       "      <th>总分</th>\n",
       "      <th>类型</th>\n",
       "      <th>所在省份</th>\n",
       "      <th>所在城市</th>\n",
       "      <th>办学方向</th>\n",
       "      <th>主管部门</th>\n",
       "    </tr>\n",
       "  </thead>\n",
       "  <tbody>\n",
       "    <tr>\n",
       "      <th>0</th>\n",
       "      <td>1</td>\n",
       "      <td>北京大学</td>\n",
       "      <td>100.00</td>\n",
       "      <td>综合</td>\n",
       "      <td>北京</td>\n",
       "      <td>北京市</td>\n",
       "      <td>中国研究型</td>\n",
       "      <td>教育部</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>1</th>\n",
       "      <td>2</td>\n",
       "      <td>清华大学</td>\n",
       "      <td>98.50</td>\n",
       "      <td>理工</td>\n",
       "      <td>北京</td>\n",
       "      <td>北京市</td>\n",
       "      <td>中国研究型</td>\n",
       "      <td>教育部</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>2</th>\n",
       "      <td>3</td>\n",
       "      <td>复旦大学</td>\n",
       "      <td>82.79</td>\n",
       "      <td>综合</td>\n",
       "      <td>上海</td>\n",
       "      <td>上海市</td>\n",
       "      <td>中国研究型</td>\n",
       "      <td>教育部</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>3</th>\n",
       "      <td>4</td>\n",
       "      <td>武汉大学</td>\n",
       "      <td>82.43</td>\n",
       "      <td>综合</td>\n",
       "      <td>湖北</td>\n",
       "      <td>武汉市</td>\n",
       "      <td>中国研究型</td>\n",
       "      <td>教育部</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>4</th>\n",
       "      <td>5</td>\n",
       "      <td>浙江大学</td>\n",
       "      <td>82.38</td>\n",
       "      <td>综合</td>\n",
       "      <td>浙江</td>\n",
       "      <td>杭州市</td>\n",
       "      <td>中国研究型</td>\n",
       "      <td>教育部</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>...</th>\n",
       "      <td>...</td>\n",
       "      <td>...</td>\n",
       "      <td>...</td>\n",
       "      <td>...</td>\n",
       "      <td>...</td>\n",
       "      <td>...</td>\n",
       "      <td>...</td>\n",
       "      <td>...</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>95</th>\n",
       "      <td>96</td>\n",
       "      <td>浙江师范大学</td>\n",
       "      <td>63.37</td>\n",
       "      <td>师范</td>\n",
       "      <td>浙江</td>\n",
       "      <td>金华市</td>\n",
       "      <td>区域特色研究型</td>\n",
       "      <td>浙江省</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>96</th>\n",
       "      <td>97</td>\n",
       "      <td>安徽大学</td>\n",
       "      <td>63.34</td>\n",
       "      <td>综合</td>\n",
       "      <td>安徽</td>\n",
       "      <td>合肥市</td>\n",
       "      <td>区域研究型</td>\n",
       "      <td>安徽省</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>97</th>\n",
       "      <td>98</td>\n",
       "      <td>首都医科大学</td>\n",
       "      <td>63.32</td>\n",
       "      <td>医药</td>\n",
       "      <td>北京</td>\n",
       "      <td>北京市</td>\n",
       "      <td>区域特色研究型</td>\n",
       "      <td>北京市</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>98</th>\n",
       "      <td>99</td>\n",
       "      <td>江南大学</td>\n",
       "      <td>63.31</td>\n",
       "      <td>综合</td>\n",
       "      <td>江苏</td>\n",
       "      <td>无锡市</td>\n",
       "      <td>区域特色研究型</td>\n",
       "      <td>教育部</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>99</th>\n",
       "      <td>100</td>\n",
       "      <td>山西大学</td>\n",
       "      <td>63.29</td>\n",
       "      <td>综合</td>\n",
       "      <td>山西</td>\n",
       "      <td>太原市</td>\n",
       "      <td>区域研究型</td>\n",
       "      <td>山西省</td>\n",
       "    </tr>\n",
       "  </tbody>\n",
       "</table>\n",
       "<p>100 rows × 8 columns</p>\n",
       "</div>"
      ]
     },
     "execution_count": 2,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "execution_count": 2
  },
  {
   "metadata": {},
   "cell_type": "markdown",
   "source": [
    "# 三、变量列的基本操作\n",
    "\n",
    "## 3.1 浏览数据"
   ],
   "id": "17e2d34ff64a298e"
  },
  {
   "metadata": {
    "ExecuteTime": {
     "end_time": "2025-09-29T14:06:45.592968Z",
     "start_time": "2025-09-29T14:06:45.587777Z"
    }
   },
   "cell_type": "code",
   "source": [
    "# 显示前5行和后5行\n",
    "print(df)"
   ],
   "id": "95c9124eb8d4ab7",
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "     名次    学校名称      总分  类型 所在省份 所在城市     办学方向 主管部门\n",
      "0     1    北京大学  100.00  综合   北京  北京市    中国研究型  教育部\n",
      "1     2    清华大学   98.50  理工   北京  北京市    中国研究型  教育部\n",
      "2     3    复旦大学   82.79  综合   上海  上海市    中国研究型  教育部\n",
      "3     4    武汉大学   82.43  综合   湖北  武汉市    中国研究型  教育部\n",
      "4     5    浙江大学   82.38  综合   浙江  杭州市    中国研究型  教育部\n",
      "..  ...     ...     ...  ..  ...  ...      ...  ...\n",
      "95   96  浙江师范大学   63.37  师范   浙江  金华市  区域特色研究型  浙江省\n",
      "96   97    安徽大学   63.34  综合   安徽  合肥市    区域研究型  安徽省\n",
      "97   98  首都医科大学   63.32  医药   北京  北京市  区域特色研究型  北京市\n",
      "98   99    江南大学   63.31  综合   江苏  无锡市  区域特色研究型  教育部\n",
      "99  100    山西大学   63.29  综合   山西  太原市    区域研究型  山西省\n",
      "\n",
      "[100 rows x 8 columns]\n"
     ]
    }
   ],
   "execution_count": 2
  },
  {
   "metadata": {
    "ExecuteTime": {
     "end_time": "2025-09-29T14:07:34.835359Z",
     "start_time": "2025-09-29T14:07:34.821803Z"
    }
   },
   "cell_type": "code",
   "source": [
    "# 查看数据框每列的基本信息\n",
    "# 类似于查看关系型数据库的列\n",
    "print(df.info())"
   ],
   "id": "cb5316f89a554acf",
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "<class 'pandas.core.frame.DataFrame'>\n",
      "RangeIndex: 100 entries, 0 to 99\n",
      "Data columns (total 8 columns):\n",
      " #   Column  Non-Null Count  Dtype  \n",
      "---  ------  --------------  -----  \n",
      " 0   名次      100 non-null    int64  \n",
      " 1   学校名称    100 non-null    object \n",
      " 2   总分      100 non-null    float64\n",
      " 3   类型      100 non-null    object \n",
      " 4   所在省份    100 non-null    object \n",
      " 5   所在城市    100 non-null    object \n",
      " 6   办学方向    100 non-null    object \n",
      " 7   主管部门    100 non-null    object \n",
      "dtypes: float64(1), int64(1), object(6)\n",
      "memory usage: 6.4+ KB\n",
      "None\n"
     ]
    }
   ],
   "execution_count": 3
  },
  {
   "metadata": {
    "ExecuteTime": {
     "end_time": "2025-09-29T14:08:47.337742Z",
     "start_time": "2025-09-29T14:08:47.334010Z"
    }
   },
   "cell_type": "code",
   "source": [
    "# 查看前n行数据，默认5\n",
    "print(df.head(3))"
   ],
   "id": "2f85b324c6d4a22b",
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "   名次  学校名称      总分  类型 所在省份 所在城市   办学方向 主管部门\n",
      "0   1  北京大学  100.00  综合   北京  北京市  中国研究型  教育部\n",
      "1   2  清华大学   98.50  理工   北京  北京市  中国研究型  教育部\n",
      "2   3  复旦大学   82.79  综合   上海  上海市  中国研究型  教育部\n"
     ]
    }
   ],
   "execution_count": 4
  },
  {
   "metadata": {
    "ExecuteTime": {
     "end_time": "2025-09-29T14:09:14.478058Z",
     "start_time": "2025-09-29T14:09:14.473936Z"
    }
   },
   "cell_type": "code",
   "source": [
    "# 查看后n行的数据，默认5\n",
    "print(df.tail())"
   ],
   "id": "8a12698cb6fa8f60",
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "     名次    学校名称     总分  类型 所在省份 所在城市     办学方向 主管部门\n",
      "95   96  浙江师范大学  63.37  师范   浙江  金华市  区域特色研究型  浙江省\n",
      "96   97    安徽大学  63.34  综合   安徽  合肥市    区域研究型  安徽省\n",
      "97   98  首都医科大学  63.32  医药   北京  北京市  区域特色研究型  北京市\n",
      "98   99    江南大学  63.31  综合   江苏  无锡市  区域特色研究型  教育部\n",
      "99  100    山西大学  63.29  综合   山西  太原市    区域研究型  山西省\n"
     ]
    }
   ],
   "execution_count": 5
  },
  {
   "metadata": {},
   "cell_type": "markdown",
   "source": [
    "## 3.2 重命名列名\n",
    "\n",
    "### 直接修改列名"
   ],
   "id": "a7e8161e8e295386"
  },
  {
   "metadata": {
    "ExecuteTime": {
     "end_time": "2025-09-29T14:13:56.677443Z",
     "start_time": "2025-09-29T14:13:56.674059Z"
    }
   },
   "cell_type": "code",
   "source": [
    "# 查看列名\n",
    "df.columns"
   ],
   "id": "3712956e647ed921",
   "outputs": [
    {
     "data": {
      "text/plain": [
       "Index(['名次', '学校名称', '总分', '类型', '所在省份', '所在城市', '办学方向', '主管部门'], dtype='object')"
      ]
     },
     "execution_count": 6,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "execution_count": 6
  },
  {
   "metadata": {
    "ExecuteTime": {
     "end_time": "2025-09-29T14:15:54.803303Z",
     "start_time": "2025-09-29T14:15:54.797229Z"
    }
   },
   "cell_type": "code",
   "source": [
    "# 重命名列，赋值的列表必须和Index对象的列表长度相同\n",
    "df.columns = ['排名', '校名', '总分', '类型', '所在省份', '所在城市', '办学方向', '主管部门']\n",
    "df"
   ],
   "id": "bc1fea23bd6d196f",
   "outputs": [
    {
     "data": {
      "text/plain": [
       "     排名      校名      总分  类型 所在省份 所在城市     办学方向 主管部门\n",
       "0     1    北京大学  100.00  综合   北京  北京市    中国研究型  教育部\n",
       "1     2    清华大学   98.50  理工   北京  北京市    中国研究型  教育部\n",
       "2     3    复旦大学   82.79  综合   上海  上海市    中国研究型  教育部\n",
       "3     4    武汉大学   82.43  综合   湖北  武汉市    中国研究型  教育部\n",
       "4     5    浙江大学   82.38  综合   浙江  杭州市    中国研究型  教育部\n",
       "..  ...     ...     ...  ..  ...  ...      ...  ...\n",
       "95   96  浙江师范大学   63.37  师范   浙江  金华市  区域特色研究型  浙江省\n",
       "96   97    安徽大学   63.34  综合   安徽  合肥市    区域研究型  安徽省\n",
       "97   98  首都医科大学   63.32  医药   北京  北京市  区域特色研究型  北京市\n",
       "98   99    江南大学   63.31  综合   江苏  无锡市  区域特色研究型  教育部\n",
       "99  100    山西大学   63.29  综合   山西  太原市    区域研究型  山西省\n",
       "\n",
       "[100 rows x 8 columns]"
      ],
      "text/html": [
       "<div>\n",
       "<style scoped>\n",
       "    .dataframe tbody tr th:only-of-type {\n",
       "        vertical-align: middle;\n",
       "    }\n",
       "\n",
       "    .dataframe tbody tr th {\n",
       "        vertical-align: top;\n",
       "    }\n",
       "\n",
       "    .dataframe thead th {\n",
       "        text-align: right;\n",
       "    }\n",
       "</style>\n",
       "<table border=\"1\" class=\"dataframe\">\n",
       "  <thead>\n",
       "    <tr style=\"text-align: right;\">\n",
       "      <th></th>\n",
       "      <th>排名</th>\n",
       "      <th>校名</th>\n",
       "      <th>总分</th>\n",
       "      <th>类型</th>\n",
       "      <th>所在省份</th>\n",
       "      <th>所在城市</th>\n",
       "      <th>办学方向</th>\n",
       "      <th>主管部门</th>\n",
       "    </tr>\n",
       "  </thead>\n",
       "  <tbody>\n",
       "    <tr>\n",
       "      <th>0</th>\n",
       "      <td>1</td>\n",
       "      <td>北京大学</td>\n",
       "      <td>100.00</td>\n",
       "      <td>综合</td>\n",
       "      <td>北京</td>\n",
       "      <td>北京市</td>\n",
       "      <td>中国研究型</td>\n",
       "      <td>教育部</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>1</th>\n",
       "      <td>2</td>\n",
       "      <td>清华大学</td>\n",
       "      <td>98.50</td>\n",
       "      <td>理工</td>\n",
       "      <td>北京</td>\n",
       "      <td>北京市</td>\n",
       "      <td>中国研究型</td>\n",
       "      <td>教育部</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>2</th>\n",
       "      <td>3</td>\n",
       "      <td>复旦大学</td>\n",
       "      <td>82.79</td>\n",
       "      <td>综合</td>\n",
       "      <td>上海</td>\n",
       "      <td>上海市</td>\n",
       "      <td>中国研究型</td>\n",
       "      <td>教育部</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>3</th>\n",
       "      <td>4</td>\n",
       "      <td>武汉大学</td>\n",
       "      <td>82.43</td>\n",
       "      <td>综合</td>\n",
       "      <td>湖北</td>\n",
       "      <td>武汉市</td>\n",
       "      <td>中国研究型</td>\n",
       "      <td>教育部</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>4</th>\n",
       "      <td>5</td>\n",
       "      <td>浙江大学</td>\n",
       "      <td>82.38</td>\n",
       "      <td>综合</td>\n",
       "      <td>浙江</td>\n",
       "      <td>杭州市</td>\n",
       "      <td>中国研究型</td>\n",
       "      <td>教育部</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>...</th>\n",
       "      <td>...</td>\n",
       "      <td>...</td>\n",
       "      <td>...</td>\n",
       "      <td>...</td>\n",
       "      <td>...</td>\n",
       "      <td>...</td>\n",
       "      <td>...</td>\n",
       "      <td>...</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>95</th>\n",
       "      <td>96</td>\n",
       "      <td>浙江师范大学</td>\n",
       "      <td>63.37</td>\n",
       "      <td>师范</td>\n",
       "      <td>浙江</td>\n",
       "      <td>金华市</td>\n",
       "      <td>区域特色研究型</td>\n",
       "      <td>浙江省</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>96</th>\n",
       "      <td>97</td>\n",
       "      <td>安徽大学</td>\n",
       "      <td>63.34</td>\n",
       "      <td>综合</td>\n",
       "      <td>安徽</td>\n",
       "      <td>合肥市</td>\n",
       "      <td>区域研究型</td>\n",
       "      <td>安徽省</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>97</th>\n",
       "      <td>98</td>\n",
       "      <td>首都医科大学</td>\n",
       "      <td>63.32</td>\n",
       "      <td>医药</td>\n",
       "      <td>北京</td>\n",
       "      <td>北京市</td>\n",
       "      <td>区域特色研究型</td>\n",
       "      <td>北京市</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>98</th>\n",
       "      <td>99</td>\n",
       "      <td>江南大学</td>\n",
       "      <td>63.31</td>\n",
       "      <td>综合</td>\n",
       "      <td>江苏</td>\n",
       "      <td>无锡市</td>\n",
       "      <td>区域特色研究型</td>\n",
       "      <td>教育部</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>99</th>\n",
       "      <td>100</td>\n",
       "      <td>山西大学</td>\n",
       "      <td>63.29</td>\n",
       "      <td>综合</td>\n",
       "      <td>山西</td>\n",
       "      <td>太原市</td>\n",
       "      <td>区域研究型</td>\n",
       "      <td>山西省</td>\n",
       "    </tr>\n",
       "  </tbody>\n",
       "</table>\n",
       "<p>100 rows × 8 columns</p>\n",
       "</div>"
      ]
     },
     "execution_count": 8,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "execution_count": 8
  },
  {
   "metadata": {},
   "cell_type": "markdown",
   "source": [
    "### 只修改指定列名\n",
    "\n",
    "语法：\n",
    "\n",
    "``` python\n",
    "df.rename(\n",
    "    columns = 新旧名称字典, #  {'旧名称': '新名称'}\n",
    "    inplace = False, #  是否直接替换原数据框\n",
    ")\n",
    "```"
   ],
   "id": "ca26aea8bd982e74"
  },
  {
   "metadata": {
    "ExecuteTime": {
     "end_time": "2025-09-29T14:18:12.652648Z",
     "start_time": "2025-09-29T14:18:12.646353Z"
    }
   },
   "cell_type": "code",
   "source": "df",
   "id": "a3d723424754a1e1",
   "outputs": [
    {
     "data": {
      "text/plain": [
       "     排名      校名      总分  类型 所在省份 所在城市     办学方向 主管部门\n",
       "0     1    北京大学  100.00  综合   北京  北京市    中国研究型  教育部\n",
       "1     2    清华大学   98.50  理工   北京  北京市    中国研究型  教育部\n",
       "2     3    复旦大学   82.79  综合   上海  上海市    中国研究型  教育部\n",
       "3     4    武汉大学   82.43  综合   湖北  武汉市    中国研究型  教育部\n",
       "4     5    浙江大学   82.38  综合   浙江  杭州市    中国研究型  教育部\n",
       "..  ...     ...     ...  ..  ...  ...      ...  ...\n",
       "95   96  浙江师范大学   63.37  师范   浙江  金华市  区域特色研究型  浙江省\n",
       "96   97    安徽大学   63.34  综合   安徽  合肥市    区域研究型  安徽省\n",
       "97   98  首都医科大学   63.32  医药   北京  北京市  区域特色研究型  北京市\n",
       "98   99    江南大学   63.31  综合   江苏  无锡市  区域特色研究型  教育部\n",
       "99  100    山西大学   63.29  综合   山西  太原市    区域研究型  山西省\n",
       "\n",
       "[100 rows x 8 columns]"
      ],
      "text/html": [
       "<div>\n",
       "<style scoped>\n",
       "    .dataframe tbody tr th:only-of-type {\n",
       "        vertical-align: middle;\n",
       "    }\n",
       "\n",
       "    .dataframe tbody tr th {\n",
       "        vertical-align: top;\n",
       "    }\n",
       "\n",
       "    .dataframe thead th {\n",
       "        text-align: right;\n",
       "    }\n",
       "</style>\n",
       "<table border=\"1\" class=\"dataframe\">\n",
       "  <thead>\n",
       "    <tr style=\"text-align: right;\">\n",
       "      <th></th>\n",
       "      <th>排名</th>\n",
       "      <th>校名</th>\n",
       "      <th>总分</th>\n",
       "      <th>类型</th>\n",
       "      <th>所在省份</th>\n",
       "      <th>所在城市</th>\n",
       "      <th>办学方向</th>\n",
       "      <th>主管部门</th>\n",
       "    </tr>\n",
       "  </thead>\n",
       "  <tbody>\n",
       "    <tr>\n",
       "      <th>0</th>\n",
       "      <td>1</td>\n",
       "      <td>北京大学</td>\n",
       "      <td>100.00</td>\n",
       "      <td>综合</td>\n",
       "      <td>北京</td>\n",
       "      <td>北京市</td>\n",
       "      <td>中国研究型</td>\n",
       "      <td>教育部</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>1</th>\n",
       "      <td>2</td>\n",
       "      <td>清华大学</td>\n",
       "      <td>98.50</td>\n",
       "      <td>理工</td>\n",
       "      <td>北京</td>\n",
       "      <td>北京市</td>\n",
       "      <td>中国研究型</td>\n",
       "      <td>教育部</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>2</th>\n",
       "      <td>3</td>\n",
       "      <td>复旦大学</td>\n",
       "      <td>82.79</td>\n",
       "      <td>综合</td>\n",
       "      <td>上海</td>\n",
       "      <td>上海市</td>\n",
       "      <td>中国研究型</td>\n",
       "      <td>教育部</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>3</th>\n",
       "      <td>4</td>\n",
       "      <td>武汉大学</td>\n",
       "      <td>82.43</td>\n",
       "      <td>综合</td>\n",
       "      <td>湖北</td>\n",
       "      <td>武汉市</td>\n",
       "      <td>中国研究型</td>\n",
       "      <td>教育部</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>4</th>\n",
       "      <td>5</td>\n",
       "      <td>浙江大学</td>\n",
       "      <td>82.38</td>\n",
       "      <td>综合</td>\n",
       "      <td>浙江</td>\n",
       "      <td>杭州市</td>\n",
       "      <td>中国研究型</td>\n",
       "      <td>教育部</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>...</th>\n",
       "      <td>...</td>\n",
       "      <td>...</td>\n",
       "      <td>...</td>\n",
       "      <td>...</td>\n",
       "      <td>...</td>\n",
       "      <td>...</td>\n",
       "      <td>...</td>\n",
       "      <td>...</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>95</th>\n",
       "      <td>96</td>\n",
       "      <td>浙江师范大学</td>\n",
       "      <td>63.37</td>\n",
       "      <td>师范</td>\n",
       "      <td>浙江</td>\n",
       "      <td>金华市</td>\n",
       "      <td>区域特色研究型</td>\n",
       "      <td>浙江省</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>96</th>\n",
       "      <td>97</td>\n",
       "      <td>安徽大学</td>\n",
       "      <td>63.34</td>\n",
       "      <td>综合</td>\n",
       "      <td>安徽</td>\n",
       "      <td>合肥市</td>\n",
       "      <td>区域研究型</td>\n",
       "      <td>安徽省</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>97</th>\n",
       "      <td>98</td>\n",
       "      <td>首都医科大学</td>\n",
       "      <td>63.32</td>\n",
       "      <td>医药</td>\n",
       "      <td>北京</td>\n",
       "      <td>北京市</td>\n",
       "      <td>区域特色研究型</td>\n",
       "      <td>北京市</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>98</th>\n",
       "      <td>99</td>\n",
       "      <td>江南大学</td>\n",
       "      <td>63.31</td>\n",
       "      <td>综合</td>\n",
       "      <td>江苏</td>\n",
       "      <td>无锡市</td>\n",
       "      <td>区域特色研究型</td>\n",
       "      <td>教育部</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>99</th>\n",
       "      <td>100</td>\n",
       "      <td>山西大学</td>\n",
       "      <td>63.29</td>\n",
       "      <td>综合</td>\n",
       "      <td>山西</td>\n",
       "      <td>太原市</td>\n",
       "      <td>区域研究型</td>\n",
       "      <td>山西省</td>\n",
       "    </tr>\n",
       "  </tbody>\n",
       "</table>\n",
       "<p>100 rows × 8 columns</p>\n",
       "</div>"
      ]
     },
     "execution_count": 9,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "execution_count": 9
  },
  {
   "metadata": {
    "ExecuteTime": {
     "end_time": "2025-09-29T14:19:57.529382Z",
     "start_time": "2025-09-29T14:19:57.523115Z"
    }
   },
   "cell_type": "code",
   "source": [
    "# 修改列名\n",
    "df2 = df.rename(columns={'校名': '学校名称', '排名': '名次'})\n",
    "df2"
   ],
   "id": "2e0fb9a0f1889480",
   "outputs": [
    {
     "data": {
      "text/plain": [
       "     名次    学校名称      总分  类型 所在省份 所在城市     办学方向 主管部门\n",
       "0     1    北京大学  100.00  综合   北京  北京市    中国研究型  教育部\n",
       "1     2    清华大学   98.50  理工   北京  北京市    中国研究型  教育部\n",
       "2     3    复旦大学   82.79  综合   上海  上海市    中国研究型  教育部\n",
       "3     4    武汉大学   82.43  综合   湖北  武汉市    中国研究型  教育部\n",
       "4     5    浙江大学   82.38  综合   浙江  杭州市    中国研究型  教育部\n",
       "..  ...     ...     ...  ..  ...  ...      ...  ...\n",
       "95   96  浙江师范大学   63.37  师范   浙江  金华市  区域特色研究型  浙江省\n",
       "96   97    安徽大学   63.34  综合   安徽  合肥市    区域研究型  安徽省\n",
       "97   98  首都医科大学   63.32  医药   北京  北京市  区域特色研究型  北京市\n",
       "98   99    江南大学   63.31  综合   江苏  无锡市  区域特色研究型  教育部\n",
       "99  100    山西大学   63.29  综合   山西  太原市    区域研究型  山西省\n",
       "\n",
       "[100 rows x 8 columns]"
      ],
      "text/html": [
       "<div>\n",
       "<style scoped>\n",
       "    .dataframe tbody tr th:only-of-type {\n",
       "        vertical-align: middle;\n",
       "    }\n",
       "\n",
       "    .dataframe tbody tr th {\n",
       "        vertical-align: top;\n",
       "    }\n",
       "\n",
       "    .dataframe thead th {\n",
       "        text-align: right;\n",
       "    }\n",
       "</style>\n",
       "<table border=\"1\" class=\"dataframe\">\n",
       "  <thead>\n",
       "    <tr style=\"text-align: right;\">\n",
       "      <th></th>\n",
       "      <th>名次</th>\n",
       "      <th>学校名称</th>\n",
       "      <th>总分</th>\n",
       "      <th>类型</th>\n",
       "      <th>所在省份</th>\n",
       "      <th>所在城市</th>\n",
       "      <th>办学方向</th>\n",
       "      <th>主管部门</th>\n",
       "    </tr>\n",
       "  </thead>\n",
       "  <tbody>\n",
       "    <tr>\n",
       "      <th>0</th>\n",
       "      <td>1</td>\n",
       "      <td>北京大学</td>\n",
       "      <td>100.00</td>\n",
       "      <td>综合</td>\n",
       "      <td>北京</td>\n",
       "      <td>北京市</td>\n",
       "      <td>中国研究型</td>\n",
       "      <td>教育部</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>1</th>\n",
       "      <td>2</td>\n",
       "      <td>清华大学</td>\n",
       "      <td>98.50</td>\n",
       "      <td>理工</td>\n",
       "      <td>北京</td>\n",
       "      <td>北京市</td>\n",
       "      <td>中国研究型</td>\n",
       "      <td>教育部</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>2</th>\n",
       "      <td>3</td>\n",
       "      <td>复旦大学</td>\n",
       "      <td>82.79</td>\n",
       "      <td>综合</td>\n",
       "      <td>上海</td>\n",
       "      <td>上海市</td>\n",
       "      <td>中国研究型</td>\n",
       "      <td>教育部</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>3</th>\n",
       "      <td>4</td>\n",
       "      <td>武汉大学</td>\n",
       "      <td>82.43</td>\n",
       "      <td>综合</td>\n",
       "      <td>湖北</td>\n",
       "      <td>武汉市</td>\n",
       "      <td>中国研究型</td>\n",
       "      <td>教育部</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>4</th>\n",
       "      <td>5</td>\n",
       "      <td>浙江大学</td>\n",
       "      <td>82.38</td>\n",
       "      <td>综合</td>\n",
       "      <td>浙江</td>\n",
       "      <td>杭州市</td>\n",
       "      <td>中国研究型</td>\n",
       "      <td>教育部</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>...</th>\n",
       "      <td>...</td>\n",
       "      <td>...</td>\n",
       "      <td>...</td>\n",
       "      <td>...</td>\n",
       "      <td>...</td>\n",
       "      <td>...</td>\n",
       "      <td>...</td>\n",
       "      <td>...</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>95</th>\n",
       "      <td>96</td>\n",
       "      <td>浙江师范大学</td>\n",
       "      <td>63.37</td>\n",
       "      <td>师范</td>\n",
       "      <td>浙江</td>\n",
       "      <td>金华市</td>\n",
       "      <td>区域特色研究型</td>\n",
       "      <td>浙江省</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>96</th>\n",
       "      <td>97</td>\n",
       "      <td>安徽大学</td>\n",
       "      <td>63.34</td>\n",
       "      <td>综合</td>\n",
       "      <td>安徽</td>\n",
       "      <td>合肥市</td>\n",
       "      <td>区域研究型</td>\n",
       "      <td>安徽省</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>97</th>\n",
       "      <td>98</td>\n",
       "      <td>首都医科大学</td>\n",
       "      <td>63.32</td>\n",
       "      <td>医药</td>\n",
       "      <td>北京</td>\n",
       "      <td>北京市</td>\n",
       "      <td>区域特色研究型</td>\n",
       "      <td>北京市</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>98</th>\n",
       "      <td>99</td>\n",
       "      <td>江南大学</td>\n",
       "      <td>63.31</td>\n",
       "      <td>综合</td>\n",
       "      <td>江苏</td>\n",
       "      <td>无锡市</td>\n",
       "      <td>区域特色研究型</td>\n",
       "      <td>教育部</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>99</th>\n",
       "      <td>100</td>\n",
       "      <td>山西大学</td>\n",
       "      <td>63.29</td>\n",
       "      <td>综合</td>\n",
       "      <td>山西</td>\n",
       "      <td>太原市</td>\n",
       "      <td>区域研究型</td>\n",
       "      <td>山西省</td>\n",
       "    </tr>\n",
       "  </tbody>\n",
       "</table>\n",
       "<p>100 rows × 8 columns</p>\n",
       "</div>"
      ]
     },
     "execution_count": 12,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "execution_count": 12
  },
  {
   "metadata": {
    "ExecuteTime": {
     "end_time": "2025-09-29T14:20:01.631880Z",
     "start_time": "2025-09-29T14:20:01.623335Z"
    }
   },
   "cell_type": "code",
   "source": "df",
   "id": "1c3b6d93e167fec0",
   "outputs": [
    {
     "data": {
      "text/plain": [
       "     排名      校名      总分  类型 所在省份 所在城市     办学方向 主管部门\n",
       "0     1    北京大学  100.00  综合   北京  北京市    中国研究型  教育部\n",
       "1     2    清华大学   98.50  理工   北京  北京市    中国研究型  教育部\n",
       "2     3    复旦大学   82.79  综合   上海  上海市    中国研究型  教育部\n",
       "3     4    武汉大学   82.43  综合   湖北  武汉市    中国研究型  教育部\n",
       "4     5    浙江大学   82.38  综合   浙江  杭州市    中国研究型  教育部\n",
       "..  ...     ...     ...  ..  ...  ...      ...  ...\n",
       "95   96  浙江师范大学   63.37  师范   浙江  金华市  区域特色研究型  浙江省\n",
       "96   97    安徽大学   63.34  综合   安徽  合肥市    区域研究型  安徽省\n",
       "97   98  首都医科大学   63.32  医药   北京  北京市  区域特色研究型  北京市\n",
       "98   99    江南大学   63.31  综合   江苏  无锡市  区域特色研究型  教育部\n",
       "99  100    山西大学   63.29  综合   山西  太原市    区域研究型  山西省\n",
       "\n",
       "[100 rows x 8 columns]"
      ],
      "text/html": [
       "<div>\n",
       "<style scoped>\n",
       "    .dataframe tbody tr th:only-of-type {\n",
       "        vertical-align: middle;\n",
       "    }\n",
       "\n",
       "    .dataframe tbody tr th {\n",
       "        vertical-align: top;\n",
       "    }\n",
       "\n",
       "    .dataframe thead th {\n",
       "        text-align: right;\n",
       "    }\n",
       "</style>\n",
       "<table border=\"1\" class=\"dataframe\">\n",
       "  <thead>\n",
       "    <tr style=\"text-align: right;\">\n",
       "      <th></th>\n",
       "      <th>排名</th>\n",
       "      <th>校名</th>\n",
       "      <th>总分</th>\n",
       "      <th>类型</th>\n",
       "      <th>所在省份</th>\n",
       "      <th>所在城市</th>\n",
       "      <th>办学方向</th>\n",
       "      <th>主管部门</th>\n",
       "    </tr>\n",
       "  </thead>\n",
       "  <tbody>\n",
       "    <tr>\n",
       "      <th>0</th>\n",
       "      <td>1</td>\n",
       "      <td>北京大学</td>\n",
       "      <td>100.00</td>\n",
       "      <td>综合</td>\n",
       "      <td>北京</td>\n",
       "      <td>北京市</td>\n",
       "      <td>中国研究型</td>\n",
       "      <td>教育部</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>1</th>\n",
       "      <td>2</td>\n",
       "      <td>清华大学</td>\n",
       "      <td>98.50</td>\n",
       "      <td>理工</td>\n",
       "      <td>北京</td>\n",
       "      <td>北京市</td>\n",
       "      <td>中国研究型</td>\n",
       "      <td>教育部</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>2</th>\n",
       "      <td>3</td>\n",
       "      <td>复旦大学</td>\n",
       "      <td>82.79</td>\n",
       "      <td>综合</td>\n",
       "      <td>上海</td>\n",
       "      <td>上海市</td>\n",
       "      <td>中国研究型</td>\n",
       "      <td>教育部</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>3</th>\n",
       "      <td>4</td>\n",
       "      <td>武汉大学</td>\n",
       "      <td>82.43</td>\n",
       "      <td>综合</td>\n",
       "      <td>湖北</td>\n",
       "      <td>武汉市</td>\n",
       "      <td>中国研究型</td>\n",
       "      <td>教育部</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>4</th>\n",
       "      <td>5</td>\n",
       "      <td>浙江大学</td>\n",
       "      <td>82.38</td>\n",
       "      <td>综合</td>\n",
       "      <td>浙江</td>\n",
       "      <td>杭州市</td>\n",
       "      <td>中国研究型</td>\n",
       "      <td>教育部</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>...</th>\n",
       "      <td>...</td>\n",
       "      <td>...</td>\n",
       "      <td>...</td>\n",
       "      <td>...</td>\n",
       "      <td>...</td>\n",
       "      <td>...</td>\n",
       "      <td>...</td>\n",
       "      <td>...</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>95</th>\n",
       "      <td>96</td>\n",
       "      <td>浙江师范大学</td>\n",
       "      <td>63.37</td>\n",
       "      <td>师范</td>\n",
       "      <td>浙江</td>\n",
       "      <td>金华市</td>\n",
       "      <td>区域特色研究型</td>\n",
       "      <td>浙江省</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>96</th>\n",
       "      <td>97</td>\n",
       "      <td>安徽大学</td>\n",
       "      <td>63.34</td>\n",
       "      <td>综合</td>\n",
       "      <td>安徽</td>\n",
       "      <td>合肥市</td>\n",
       "      <td>区域研究型</td>\n",
       "      <td>安徽省</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>97</th>\n",
       "      <td>98</td>\n",
       "      <td>首都医科大学</td>\n",
       "      <td>63.32</td>\n",
       "      <td>医药</td>\n",
       "      <td>北京</td>\n",
       "      <td>北京市</td>\n",
       "      <td>区域特色研究型</td>\n",
       "      <td>北京市</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>98</th>\n",
       "      <td>99</td>\n",
       "      <td>江南大学</td>\n",
       "      <td>63.31</td>\n",
       "      <td>综合</td>\n",
       "      <td>江苏</td>\n",
       "      <td>无锡市</td>\n",
       "      <td>区域特色研究型</td>\n",
       "      <td>教育部</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>99</th>\n",
       "      <td>100</td>\n",
       "      <td>山西大学</td>\n",
       "      <td>63.29</td>\n",
       "      <td>综合</td>\n",
       "      <td>山西</td>\n",
       "      <td>太原市</td>\n",
       "      <td>区域研究型</td>\n",
       "      <td>山西省</td>\n",
       "    </tr>\n",
       "  </tbody>\n",
       "</table>\n",
       "<p>100 rows × 8 columns</p>\n",
       "</div>"
      ]
     },
     "execution_count": 13,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "execution_count": 13
  },
  {
   "metadata": {},
   "cell_type": "markdown",
   "source": [
    "## 3.3 筛选变量列\n",
    "\n",
    "### 点号写法\n",
    "\n",
    "语法：\n",
    "\n",
    "``` python\n",
    "df.列名\n",
    "```\n",
    "\n",
    "虽然这种写法很简单，但是只适用于已存在的列，只能筛选单列，结果为Series"
   ],
   "id": "c4a66b5c9eed2d7f"
  },
  {
   "metadata": {
    "ExecuteTime": {
     "end_time": "2025-09-29T14:27:09.734717Z",
     "start_time": "2025-09-29T14:27:09.726905Z"
    }
   },
   "cell_type": "code",
   "source": "df.校名",
   "id": "b2c143d4ccc9184b",
   "outputs": [
    {
     "data": {
      "text/plain": [
       "0       北京大学\n",
       "1       清华大学\n",
       "2       复旦大学\n",
       "3       武汉大学\n",
       "4       浙江大学\n",
       "       ...  \n",
       "95    浙江师范大学\n",
       "96      安徽大学\n",
       "97    首都医科大学\n",
       "98      江南大学\n",
       "99      山西大学\n",
       "Name: 校名, Length: 100, dtype: object"
      ]
     },
     "execution_count": 15,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "execution_count": 15
  },
  {
   "metadata": {},
   "cell_type": "markdown",
   "source": [
    "### 方括号写法\n",
    "\n",
    "- 筛选单列 `df['列名']` 返回Series序列\n",
    "- 筛选多列 `df[Pyhon列表]` 返回DataFrame数据框"
   ],
   "id": "2316ff22bafca964"
  },
  {
   "metadata": {
    "ExecuteTime": {
     "end_time": "2025-09-29T14:29:55.353937Z",
     "start_time": "2025-09-29T14:29:55.350257Z"
    }
   },
   "cell_type": "code",
   "source": [
    "# 筛选单列\n",
    "df['所在省份']"
   ],
   "id": "fab548117e782dc9",
   "outputs": [
    {
     "data": {
      "text/plain": [
       "0     北京\n",
       "1     北京\n",
       "2     上海\n",
       "3     湖北\n",
       "4     浙江\n",
       "      ..\n",
       "95    浙江\n",
       "96    安徽\n",
       "97    北京\n",
       "98    江苏\n",
       "99    山西\n",
       "Name: 所在省份, Length: 100, dtype: object"
      ]
     },
     "execution_count": 16,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "execution_count": 16
  },
  {
   "metadata": {
    "ExecuteTime": {
     "end_time": "2025-09-29T14:31:00.389952Z",
     "start_time": "2025-09-29T14:31:00.385052Z"
    }
   },
   "cell_type": "code",
   "source": [
    "# 筛选多列\n",
    "df[['所在省份', '校名']]"
   ],
   "id": "6a5fe9cbd85255f1",
   "outputs": [
    {
     "data": {
      "text/plain": [
       "   所在省份      校名\n",
       "0    北京    北京大学\n",
       "1    北京    清华大学\n",
       "2    上海    复旦大学\n",
       "3    湖北    武汉大学\n",
       "4    浙江    浙江大学\n",
       "..  ...     ...\n",
       "95   浙江  浙江师范大学\n",
       "96   安徽    安徽大学\n",
       "97   北京  首都医科大学\n",
       "98   江苏    江南大学\n",
       "99   山西    山西大学\n",
       "\n",
       "[100 rows x 2 columns]"
      ],
      "text/html": [
       "<div>\n",
       "<style scoped>\n",
       "    .dataframe tbody tr th:only-of-type {\n",
       "        vertical-align: middle;\n",
       "    }\n",
       "\n",
       "    .dataframe tbody tr th {\n",
       "        vertical-align: top;\n",
       "    }\n",
       "\n",
       "    .dataframe thead th {\n",
       "        text-align: right;\n",
       "    }\n",
       "</style>\n",
       "<table border=\"1\" class=\"dataframe\">\n",
       "  <thead>\n",
       "    <tr style=\"text-align: right;\">\n",
       "      <th></th>\n",
       "      <th>所在省份</th>\n",
       "      <th>校名</th>\n",
       "    </tr>\n",
       "  </thead>\n",
       "  <tbody>\n",
       "    <tr>\n",
       "      <th>0</th>\n",
       "      <td>北京</td>\n",
       "      <td>北京大学</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>1</th>\n",
       "      <td>北京</td>\n",
       "      <td>清华大学</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>2</th>\n",
       "      <td>上海</td>\n",
       "      <td>复旦大学</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>3</th>\n",
       "      <td>湖北</td>\n",
       "      <td>武汉大学</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>4</th>\n",
       "      <td>浙江</td>\n",
       "      <td>浙江大学</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>...</th>\n",
       "      <td>...</td>\n",
       "      <td>...</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>95</th>\n",
       "      <td>浙江</td>\n",
       "      <td>浙江师范大学</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>96</th>\n",
       "      <td>安徽</td>\n",
       "      <td>安徽大学</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>97</th>\n",
       "      <td>北京</td>\n",
       "      <td>首都医科大学</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>98</th>\n",
       "      <td>江苏</td>\n",
       "      <td>江南大学</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>99</th>\n",
       "      <td>山西</td>\n",
       "      <td>山西大学</td>\n",
       "    </tr>\n",
       "  </tbody>\n",
       "</table>\n",
       "<p>100 rows × 2 columns</p>\n",
       "</div>"
      ]
     },
     "execution_count": 18,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "execution_count": 18
  },
  {
   "metadata": {},
   "cell_type": "markdown",
   "source": [
    "## 3.4 删除列\n",
    "\n",
    "语法：\n",
    "\n",
    "``` python\n",
    "df.drop(\n",
    "    index = 行标签, # 删除指定行\n",
    "    columns = 列标签, # 删除指定列，多个时用列表形式提供\n",
    "    inplace = False, # 是否直接更改原数据框\n",
    ")\n",
    "````"
   ],
   "id": "988ec716e48fd7e2"
  },
  {
   "metadata": {
    "ExecuteTime": {
     "end_time": "2025-09-29T14:36:43.813912Z",
     "start_time": "2025-09-29T14:36:43.804334Z"
    }
   },
   "cell_type": "code",
   "source": "df.drop(columns='所在省份')",
   "id": "cf6c7c3fe7f730fb",
   "outputs": [
    {
     "data": {
      "text/plain": [
       "     排名      校名      总分  类型 所在城市     办学方向 主管部门\n",
       "0     1    北京大学  100.00  综合  北京市    中国研究型  教育部\n",
       "1     2    清华大学   98.50  理工  北京市    中国研究型  教育部\n",
       "2     3    复旦大学   82.79  综合  上海市    中国研究型  教育部\n",
       "3     4    武汉大学   82.43  综合  武汉市    中国研究型  教育部\n",
       "4     5    浙江大学   82.38  综合  杭州市    中国研究型  教育部\n",
       "..  ...     ...     ...  ..  ...      ...  ...\n",
       "95   96  浙江师范大学   63.37  师范  金华市  区域特色研究型  浙江省\n",
       "96   97    安徽大学   63.34  综合  合肥市    区域研究型  安徽省\n",
       "97   98  首都医科大学   63.32  医药  北京市  区域特色研究型  北京市\n",
       "98   99    江南大学   63.31  综合  无锡市  区域特色研究型  教育部\n",
       "99  100    山西大学   63.29  综合  太原市    区域研究型  山西省\n",
       "\n",
       "[100 rows x 7 columns]"
      ],
      "text/html": [
       "<div>\n",
       "<style scoped>\n",
       "    .dataframe tbody tr th:only-of-type {\n",
       "        vertical-align: middle;\n",
       "    }\n",
       "\n",
       "    .dataframe tbody tr th {\n",
       "        vertical-align: top;\n",
       "    }\n",
       "\n",
       "    .dataframe thead th {\n",
       "        text-align: right;\n",
       "    }\n",
       "</style>\n",
       "<table border=\"1\" class=\"dataframe\">\n",
       "  <thead>\n",
       "    <tr style=\"text-align: right;\">\n",
       "      <th></th>\n",
       "      <th>排名</th>\n",
       "      <th>校名</th>\n",
       "      <th>总分</th>\n",
       "      <th>类型</th>\n",
       "      <th>所在城市</th>\n",
       "      <th>办学方向</th>\n",
       "      <th>主管部门</th>\n",
       "    </tr>\n",
       "  </thead>\n",
       "  <tbody>\n",
       "    <tr>\n",
       "      <th>0</th>\n",
       "      <td>1</td>\n",
       "      <td>北京大学</td>\n",
       "      <td>100.00</td>\n",
       "      <td>综合</td>\n",
       "      <td>北京市</td>\n",
       "      <td>中国研究型</td>\n",
       "      <td>教育部</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>1</th>\n",
       "      <td>2</td>\n",
       "      <td>清华大学</td>\n",
       "      <td>98.50</td>\n",
       "      <td>理工</td>\n",
       "      <td>北京市</td>\n",
       "      <td>中国研究型</td>\n",
       "      <td>教育部</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>2</th>\n",
       "      <td>3</td>\n",
       "      <td>复旦大学</td>\n",
       "      <td>82.79</td>\n",
       "      <td>综合</td>\n",
       "      <td>上海市</td>\n",
       "      <td>中国研究型</td>\n",
       "      <td>教育部</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>3</th>\n",
       "      <td>4</td>\n",
       "      <td>武汉大学</td>\n",
       "      <td>82.43</td>\n",
       "      <td>综合</td>\n",
       "      <td>武汉市</td>\n",
       "      <td>中国研究型</td>\n",
       "      <td>教育部</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>4</th>\n",
       "      <td>5</td>\n",
       "      <td>浙江大学</td>\n",
       "      <td>82.38</td>\n",
       "      <td>综合</td>\n",
       "      <td>杭州市</td>\n",
       "      <td>中国研究型</td>\n",
       "      <td>教育部</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>...</th>\n",
       "      <td>...</td>\n",
       "      <td>...</td>\n",
       "      <td>...</td>\n",
       "      <td>...</td>\n",
       "      <td>...</td>\n",
       "      <td>...</td>\n",
       "      <td>...</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>95</th>\n",
       "      <td>96</td>\n",
       "      <td>浙江师范大学</td>\n",
       "      <td>63.37</td>\n",
       "      <td>师范</td>\n",
       "      <td>金华市</td>\n",
       "      <td>区域特色研究型</td>\n",
       "      <td>浙江省</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>96</th>\n",
       "      <td>97</td>\n",
       "      <td>安徽大学</td>\n",
       "      <td>63.34</td>\n",
       "      <td>综合</td>\n",
       "      <td>合肥市</td>\n",
       "      <td>区域研究型</td>\n",
       "      <td>安徽省</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>97</th>\n",
       "      <td>98</td>\n",
       "      <td>首都医科大学</td>\n",
       "      <td>63.32</td>\n",
       "      <td>医药</td>\n",
       "      <td>北京市</td>\n",
       "      <td>区域特色研究型</td>\n",
       "      <td>北京市</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>98</th>\n",
       "      <td>99</td>\n",
       "      <td>江南大学</td>\n",
       "      <td>63.31</td>\n",
       "      <td>综合</td>\n",
       "      <td>无锡市</td>\n",
       "      <td>区域特色研究型</td>\n",
       "      <td>教育部</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>99</th>\n",
       "      <td>100</td>\n",
       "      <td>山西大学</td>\n",
       "      <td>63.29</td>\n",
       "      <td>综合</td>\n",
       "      <td>太原市</td>\n",
       "      <td>区域研究型</td>\n",
       "      <td>山西省</td>\n",
       "    </tr>\n",
       "  </tbody>\n",
       "</table>\n",
       "<p>100 rows × 7 columns</p>\n",
       "</div>"
      ]
     },
     "execution_count": 19,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "execution_count": 19
  },
  {
   "metadata": {},
   "cell_type": "markdown",
   "source": [
    "删除指定列\n",
    "\n",
    "``` python\n",
    "del df['column-name']\n",
    "```\n",
    "\n",
    "这种写法直接修改原数据框，不建议使用"
   ],
   "id": "bdffff83286937a3"
  },
  {
   "metadata": {
    "ExecuteTime": {
     "end_time": "2025-09-29T14:39:17.225609Z",
     "start_time": "2025-09-29T14:39:17.217988Z"
    }
   },
   "cell_type": "code",
   "source": [
    "del df['所在省份']\n",
    "df"
   ],
   "id": "a6a709c651240204",
   "outputs": [
    {
     "data": {
      "text/plain": [
       "        校名      总分  类型     办学方向 主管部门\n",
       "0     北京大学  100.00  综合    中国研究型  教育部\n",
       "1     清华大学   98.50  理工    中国研究型  教育部\n",
       "2     复旦大学   82.79  综合    中国研究型  教育部\n",
       "3     武汉大学   82.43  综合    中国研究型  教育部\n",
       "4     浙江大学   82.38  综合    中国研究型  教育部\n",
       "..     ...     ...  ..      ...  ...\n",
       "95  浙江师范大学   63.37  师范  区域特色研究型  浙江省\n",
       "96    安徽大学   63.34  综合    区域研究型  安徽省\n",
       "97  首都医科大学   63.32  医药  区域特色研究型  北京市\n",
       "98    江南大学   63.31  综合  区域特色研究型  教育部\n",
       "99    山西大学   63.29  综合    区域研究型  山西省\n",
       "\n",
       "[100 rows x 5 columns]"
      ],
      "text/html": [
       "<div>\n",
       "<style scoped>\n",
       "    .dataframe tbody tr th:only-of-type {\n",
       "        vertical-align: middle;\n",
       "    }\n",
       "\n",
       "    .dataframe tbody tr th {\n",
       "        vertical-align: top;\n",
       "    }\n",
       "\n",
       "    .dataframe thead th {\n",
       "        text-align: right;\n",
       "    }\n",
       "</style>\n",
       "<table border=\"1\" class=\"dataframe\">\n",
       "  <thead>\n",
       "    <tr style=\"text-align: right;\">\n",
       "      <th></th>\n",
       "      <th>校名</th>\n",
       "      <th>总分</th>\n",
       "      <th>类型</th>\n",
       "      <th>办学方向</th>\n",
       "      <th>主管部门</th>\n",
       "    </tr>\n",
       "  </thead>\n",
       "  <tbody>\n",
       "    <tr>\n",
       "      <th>0</th>\n",
       "      <td>北京大学</td>\n",
       "      <td>100.00</td>\n",
       "      <td>综合</td>\n",
       "      <td>中国研究型</td>\n",
       "      <td>教育部</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>1</th>\n",
       "      <td>清华大学</td>\n",
       "      <td>98.50</td>\n",
       "      <td>理工</td>\n",
       "      <td>中国研究型</td>\n",
       "      <td>教育部</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>2</th>\n",
       "      <td>复旦大学</td>\n",
       "      <td>82.79</td>\n",
       "      <td>综合</td>\n",
       "      <td>中国研究型</td>\n",
       "      <td>教育部</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>3</th>\n",
       "      <td>武汉大学</td>\n",
       "      <td>82.43</td>\n",
       "      <td>综合</td>\n",
       "      <td>中国研究型</td>\n",
       "      <td>教育部</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>4</th>\n",
       "      <td>浙江大学</td>\n",
       "      <td>82.38</td>\n",
       "      <td>综合</td>\n",
       "      <td>中国研究型</td>\n",
       "      <td>教育部</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>...</th>\n",
       "      <td>...</td>\n",
       "      <td>...</td>\n",
       "      <td>...</td>\n",
       "      <td>...</td>\n",
       "      <td>...</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>95</th>\n",
       "      <td>浙江师范大学</td>\n",
       "      <td>63.37</td>\n",
       "      <td>师范</td>\n",
       "      <td>区域特色研究型</td>\n",
       "      <td>浙江省</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>96</th>\n",
       "      <td>安徽大学</td>\n",
       "      <td>63.34</td>\n",
       "      <td>综合</td>\n",
       "      <td>区域研究型</td>\n",
       "      <td>安徽省</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>97</th>\n",
       "      <td>首都医科大学</td>\n",
       "      <td>63.32</td>\n",
       "      <td>医药</td>\n",
       "      <td>区域特色研究型</td>\n",
       "      <td>北京市</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>98</th>\n",
       "      <td>江南大学</td>\n",
       "      <td>63.31</td>\n",
       "      <td>综合</td>\n",
       "      <td>区域特色研究型</td>\n",
       "      <td>教育部</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>99</th>\n",
       "      <td>山西大学</td>\n",
       "      <td>63.29</td>\n",
       "      <td>综合</td>\n",
       "      <td>区域研究型</td>\n",
       "      <td>山西省</td>\n",
       "    </tr>\n",
       "  </tbody>\n",
       "</table>\n",
       "<p>100 rows × 5 columns</p>\n",
       "</div>"
      ]
     },
     "execution_count": 23,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "execution_count": 23
  },
  {
   "metadata": {},
   "cell_type": "markdown",
   "source": [
    "## 3.5 根据列的数据类型筛选\n",
    "\n",
    "语法：\n",
    "\n",
    "``` python\n",
    "df.select_dtypes(\n",
    "    include = None, # 考虑纳入的变量类型，注意数值类型可能对应了多种关键字\n",
    "    exclude = None, # 考虑剔除的变量类型\n",
    ")\n",
    "```"
   ],
   "id": "6a0402c0db440aef"
  },
  {
   "metadata": {
    "ExecuteTime": {
     "end_time": "2025-09-30T02:42:30.922336Z",
     "start_time": "2025-09-30T02:42:30.916717Z"
    }
   },
   "cell_type": "code",
   "source": [
    "# 筛选数据类型为float的列\n",
    "df.select_dtypes('float').head(2)"
   ],
   "id": "4c2c37da5e37c9f2",
   "outputs": [
    {
     "data": {
      "text/plain": [
       "      总分\n",
       "0  100.0\n",
       "1   98.5"
      ],
      "text/html": [
       "<div>\n",
       "<style scoped>\n",
       "    .dataframe tbody tr th:only-of-type {\n",
       "        vertical-align: middle;\n",
       "    }\n",
       "\n",
       "    .dataframe tbody tr th {\n",
       "        vertical-align: top;\n",
       "    }\n",
       "\n",
       "    .dataframe thead th {\n",
       "        text-align: right;\n",
       "    }\n",
       "</style>\n",
       "<table border=\"1\" class=\"dataframe\">\n",
       "  <thead>\n",
       "    <tr style=\"text-align: right;\">\n",
       "      <th></th>\n",
       "      <th>总分</th>\n",
       "    </tr>\n",
       "  </thead>\n",
       "  <tbody>\n",
       "    <tr>\n",
       "      <th>0</th>\n",
       "      <td>100.0</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>1</th>\n",
       "      <td>98.5</td>\n",
       "    </tr>\n",
       "  </tbody>\n",
       "</table>\n",
       "</div>"
      ]
     },
     "execution_count": 4,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "execution_count": 4
  },
  {
   "metadata": {
    "ExecuteTime": {
     "end_time": "2025-09-30T02:43:18.781654Z",
     "start_time": "2025-09-30T02:43:18.774419Z"
    }
   },
   "cell_type": "code",
   "source": [
    "# 筛选数据类型是number的列\n",
    "# 推荐使用nunber类型，少用int或float类型筛选数字列\n",
    "df.select_dtypes('number').head(2)"
   ],
   "id": "98a97f77fe85edc7",
   "outputs": [
    {
     "data": {
      "text/plain": [
       "   名次     总分\n",
       "0   1  100.0\n",
       "1   2   98.5"
      ],
      "text/html": [
       "<div>\n",
       "<style scoped>\n",
       "    .dataframe tbody tr th:only-of-type {\n",
       "        vertical-align: middle;\n",
       "    }\n",
       "\n",
       "    .dataframe tbody tr th {\n",
       "        vertical-align: top;\n",
       "    }\n",
       "\n",
       "    .dataframe thead th {\n",
       "        text-align: right;\n",
       "    }\n",
       "</style>\n",
       "<table border=\"1\" class=\"dataframe\">\n",
       "  <thead>\n",
       "    <tr style=\"text-align: right;\">\n",
       "      <th></th>\n",
       "      <th>名次</th>\n",
       "      <th>总分</th>\n",
       "    </tr>\n",
       "  </thead>\n",
       "  <tbody>\n",
       "    <tr>\n",
       "      <th>0</th>\n",
       "      <td>1</td>\n",
       "      <td>100.0</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>1</th>\n",
       "      <td>2</td>\n",
       "      <td>98.5</td>\n",
       "    </tr>\n",
       "  </tbody>\n",
       "</table>\n",
       "</div>"
      ]
     },
     "execution_count": 5,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "execution_count": 5
  },
  {
   "metadata": {
    "ExecuteTime": {
     "end_time": "2025-09-30T02:45:09.279184Z",
     "start_time": "2025-09-30T02:45:09.273680Z"
    }
   },
   "cell_type": "code",
   "source": [
    "# 筛选数据类型不是number的列\n",
    "df.select_dtypes(exclude='number').head(3)"
   ],
   "id": "b32ea108bb94e5b2",
   "outputs": [
    {
     "data": {
      "text/plain": [
       "   学校名称  类型 所在省份 所在城市   办学方向 主管部门\n",
       "0  北京大学  综合   北京  北京市  中国研究型  教育部\n",
       "1  清华大学  理工   北京  北京市  中国研究型  教育部\n",
       "2  复旦大学  综合   上海  上海市  中国研究型  教育部"
      ],
      "text/html": [
       "<div>\n",
       "<style scoped>\n",
       "    .dataframe tbody tr th:only-of-type {\n",
       "        vertical-align: middle;\n",
       "    }\n",
       "\n",
       "    .dataframe tbody tr th {\n",
       "        vertical-align: top;\n",
       "    }\n",
       "\n",
       "    .dataframe thead th {\n",
       "        text-align: right;\n",
       "    }\n",
       "</style>\n",
       "<table border=\"1\" class=\"dataframe\">\n",
       "  <thead>\n",
       "    <tr style=\"text-align: right;\">\n",
       "      <th></th>\n",
       "      <th>学校名称</th>\n",
       "      <th>类型</th>\n",
       "      <th>所在省份</th>\n",
       "      <th>所在城市</th>\n",
       "      <th>办学方向</th>\n",
       "      <th>主管部门</th>\n",
       "    </tr>\n",
       "  </thead>\n",
       "  <tbody>\n",
       "    <tr>\n",
       "      <th>0</th>\n",
       "      <td>北京大学</td>\n",
       "      <td>综合</td>\n",
       "      <td>北京</td>\n",
       "      <td>北京市</td>\n",
       "      <td>中国研究型</td>\n",
       "      <td>教育部</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>1</th>\n",
       "      <td>清华大学</td>\n",
       "      <td>理工</td>\n",
       "      <td>北京</td>\n",
       "      <td>北京市</td>\n",
       "      <td>中国研究型</td>\n",
       "      <td>教育部</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>2</th>\n",
       "      <td>复旦大学</td>\n",
       "      <td>综合</td>\n",
       "      <td>上海</td>\n",
       "      <td>上海市</td>\n",
       "      <td>中国研究型</td>\n",
       "      <td>教育部</td>\n",
       "    </tr>\n",
       "  </tbody>\n",
       "</table>\n",
       "</div>"
      ]
     },
     "execution_count": 6,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "execution_count": 6
  },
  {
   "metadata": {
    "ExecuteTime": {
     "end_time": "2025-09-30T02:46:15.642164Z",
     "start_time": "2025-09-30T02:46:15.637501Z"
    }
   },
   "cell_type": "code",
   "source": [
    "# 筛选数据类型是float和int的列\n",
    "df.select_dtypes(include=['float', 'int']).head(3)"
   ],
   "id": "ecdc0dda120d899a",
   "outputs": [
    {
     "data": {
      "text/plain": [
       "   名次      总分\n",
       "0   1  100.00\n",
       "1   2   98.50\n",
       "2   3   82.79"
      ],
      "text/html": [
       "<div>\n",
       "<style scoped>\n",
       "    .dataframe tbody tr th:only-of-type {\n",
       "        vertical-align: middle;\n",
       "    }\n",
       "\n",
       "    .dataframe tbody tr th {\n",
       "        vertical-align: top;\n",
       "    }\n",
       "\n",
       "    .dataframe thead th {\n",
       "        text-align: right;\n",
       "    }\n",
       "</style>\n",
       "<table border=\"1\" class=\"dataframe\">\n",
       "  <thead>\n",
       "    <tr style=\"text-align: right;\">\n",
       "      <th></th>\n",
       "      <th>名次</th>\n",
       "      <th>总分</th>\n",
       "    </tr>\n",
       "  </thead>\n",
       "  <tbody>\n",
       "    <tr>\n",
       "      <th>0</th>\n",
       "      <td>1</td>\n",
       "      <td>100.00</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>1</th>\n",
       "      <td>2</td>\n",
       "      <td>98.50</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>2</th>\n",
       "      <td>3</td>\n",
       "      <td>82.79</td>\n",
       "    </tr>\n",
       "  </tbody>\n",
       "</table>\n",
       "</div>"
      ]
     },
     "execution_count": 7,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "execution_count": 7
  }
 ],
 "metadata": {
  "kernelspec": {
   "display_name": "Python [conda env:base] *",
   "language": "python",
   "name": "conda-base-py"
  },
  "language_info": {
   "codemirror_mode": {
    "name": "ipython",
    "version": 2
   },
   "file_extension": ".py",
   "mimetype": "text/x-python",
   "name": "python",
   "nbconvert_exporter": "python",
   "pygments_lexer": "ipython2",
   "version": "2.7.6"
  }
 },
 "nbformat": 4,
 "nbformat_minor": 5
}
